package com.shambles.weixin.controller;

import java.io.ByteArrayInputStream;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.shambles.manage.model.entity.ImageData;
import com.shambles.manage.singleton.Singleton;
import com.shambles.manage.tools.CopyFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 返回图片
 */
@Controller
@RequestMapping("/mobile/flow")
public class FlowController {

	 
	@RequestMapping(value="/findimagedata")
	public @ResponseBody List findImageData(HttpServletRequest request,@RequestParam String id){
		
		LinkedHashMap<String, List<ImageData>> imageDataMap= Singleton.getInstance().getImageDataMap();
    	List<ImageData> imageDataList=new ArrayList<>();
    	try{
    		imageDataList=imageDataMap.get(id);
    	}catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
    		
		}
    	
    	return flowImageData(request, imageDataList);

	}


	public static List flowImageData(HttpServletRequest request,List list) {
		// TODO Auto-generated method stub
		String pathRoot = System.getProperty("user.home");

		String filePath=pathRoot+Singleton.filePath;

		String imgPath=request.getSession().getServletContext().getRealPath(Singleton.filePath);

		Iterator<ImageData> iterator=list.iterator();

		List fileBytes=new ArrayList<>();

		while (iterator.hasNext()) {

			ImageData imageData=iterator.next();

			try{
				String oldFile=filePath+"\\"+imageData.getURI();

				CopyFile.set(imgPath, oldFile, imageData.getURI());

				Map<String,String> map=new HashMap<>();

				map.put("name", imageData.getName());
				map.put("uri", Singleton.filePath+"\\"+imageData.getURI());
				map.put("compressUri", Singleton.filePath+"\\compressFile\\"+imageData.getURI());
				map.put("date", imageData.getDate().toString());

				fileBytes.add(map);
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

		return fileBytes;

	}
	
}
